﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZF.BLL.Page;
using GCELibrary.Current;
using ZF.BLL;
using ZF.DAL;
namespace ZF.WS
{
    public partial class Comment : PageBase
    {
        [PageArgument(Key = "title",DefaultValue="留言")]
        public string _Title { get; set; }
        public string Class { get; set; }
        public string style { get; set; }
        [PageArgument(Key = "ct", CanNull = false)]
        public string CT
        {
            get
            {
                return CommentType.ToString();
            }
            set
            {
                var names = Enum.GetNames(typeof(CommentType));
                foreach (var name in names)
                {
                    if (name.EqIgnoreCase(value))
                    {
                        CommentType = name.Convert<CommentType>();
                        return;
                    }
                }
                throw WarnFactory.CreateWarn("参数CT错误");
            }
        }
        [PageArgument(Key = "oid",CanNull=false)]
        public int ObjId { get; set; }





        private CommentType CommentType = CommentType.None;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControls();
            }
        }

        public void InitControls()
        {
            using (var dc = SiteInfo.CreateDBDataContext())
            {
                var list = dc.Sys_Comment.Where(p => p.ObjId == this.ObjId && p.Type == (int)this.CommentType).ToList();
                this.lvList.DataSource = list.OrderByDescending(p=>p.CommentId);
                this.lvList.DataBind();
                var member = SiteInfo.GetCurrentMember();
                this.tbName.Text = member == null ? "游客" : member.Account;
            }
        }

        public void Add()
        {
            InitControls();

        }
        public void AddSubmit(object sender, EventArgs e)
        {
            
            if (!this.vcodeimg1.Check())
            {
                WarnFactory.CreateWarn(new PageWarnParameter()
                {
                    JscriptMode = true,
                    Link = WarnLink.GoBack,
                    Message = "验证码错误",
                }).Alert();
                return;
            }

            using (var dc = SiteInfo.CreateDBDataContext())
            {
                var comment = new ZF.Model.Sys_Comment()
                {
                    AddTime = DateTime.Now,
                    Content = this.tbContent.Text,
                    Ip = Request.UserHostAddress,
                    ObjId = ObjId,
                    Type = (int)CommentType,
                    Name = this.tbName.Text,
                };
                SiteInfo.CheckFilterWords(comment);
                try
                {
                   
                    dc.Sys_Comment.InsertOnSubmit(comment);
                    dc.SubmitChanges();

                }
                catch (Exception ex)
                {
                    throw WarnFactory.CreateWarn(ex);
                }

                WarnFactory.CreateWarn(new PageWarnParameter()
                {
                    JscriptMode = true,
                    Message = "操作成功",
                    Link = WarnLink.Current,
                }).Alert();
            }



        }

        //public void GetObjById(int type,int objId,
    }
}
